package cn.shuto.liferay.plugin.chart.service.permission;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.staging.permission.StagingPermissionUtil;
import com.liferay.portal.security.auth.PrincipalException;
import com.liferay.portal.security.permission.PermissionChecker;

public class ChartAdminPermission {
	public static final String RESOURCE_NAME =
			"cn.shuto.liferay.plugin.chart.service.model";

		public static void check(
				PermissionChecker permissionChecker, long groupId, String actionId)
			throws PortalException {

			if (!contains(permissionChecker, groupId, actionId)) {
				throw new PrincipalException();
			}
		}

		public static boolean contains(
			PermissionChecker permissionChecker, long groupId, String actionId) {

			Boolean hasPermission = StagingPermissionUtil.hasPermission(
				permissionChecker, groupId, RESOURCE_NAME, groupId,
				"chart-admin", actionId);

			if (hasPermission != null) {
				return hasPermission.booleanValue();
			}

			return permissionChecker.hasPermission(
				groupId, RESOURCE_NAME, groupId, actionId);
		}
}
